package com.jzo2o.foundations.service.impl;

import cn.hutool.core.io.BufferUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.api.foundations.dto.response.ConfigRegionInnerResDTO;
import com.jzo2o.api.foundations.dto.response.ServeItemResDTO;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service//这里是显示这个界面的代码
public class HomeServiceImpl implements HomeService {
    @Resource
    private ServeMapper serveMapper;
    @Resource
    private IRegionService regionService;
    @Resource
    private IServeItemService serveItemService;
    @Caching(
            cacheable = {
                    //result为null时,属于缓存穿透情况，缓存时间30分钟
                    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size() != 0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
                    //result不为null时,永久缓存
                    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size() == 0", cacheManager = RedisConstants.CacheManager.FOREVER)
            }
    )
    //根据区域id获取服务图标信息
    @Override
    public List<ServeCategoryResDTO> queryServeIconCategoryByRegionIdCache(Long regionId) {
        //查询区域信息
        Region region = regionService.getById(regionId);
        //判断区域有没有启动
        if(ObjectUtil.isNull(region)||region.getActiveStatus()!= FoundationStatusEnum.ENABLE.getStatus()){
            return Collections.emptyList();
        }
        //查询首页服务列表  拿所有的服务项列表
        List<ServeCategoryResDTO> list= serveMapper.findServeIconCategoryByRegionId(regionId);
        if(CollUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        //对查询到的数据进行处理  取所有服务项列表前两个
        int endIndex = list.size() >= 2 ? 2 : list.size();
        //最多包含两个服务类型
       List<ServeCategoryResDTO> serveCategoryResDTO = new ArrayList<>(list.subList(0, endIndex));
       serveCategoryResDTO.forEach(item->{
           //取服务项下面所有的服务列表
           List<ServeSimpleResDTO> serveResDTOList = item.getServeResDTOList();
           int endIndex2 = serveResDTOList.size() >= 4 ? 4 : serveResDTOList.size();
           //最多4个服务点  拿所有服务列表的前两个
           List<ServeSimpleResDTO> serveSimpleResDTOS = new ArrayList<>(serveResDTOList.subList(0, endIndex2));
           item.setServeResDTOList(serveSimpleResDTOS);
       });

        //获取服务信息
        return serveCategoryResDTO;

    }
    @Caching(
            cacheable = {
                    //result为null时,属于缓存穿透情况，缓存时间30分钟
                    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size() != 0", cacheManager = RedisConstants.CacheManager.THIRTY_MINUTES),
                    //result不为null时,永久缓存
                    @Cacheable(value = RedisConstants.CacheName.SERVE_ICON, key = "#regionId", unless = "#result.size() == 0", cacheManager = RedisConstants.CacheManager.FOREVER)
            }
    )
    //根据区域id获取全部服务图标信息
    @Override
    public List<ServeCategoryResDTO> queryServeIconCategoryByRegionIdAll(Long regionId) {
        //查询区域信息
        Region region = regionService.getById(regionId);
        //判断区域有没有启动
        if(ObjectUtil.isNull(region)||region.getActiveStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            return Collections.emptyList();
        }
        //查询所有服务列表
        List<ServeCategoryResDTO> list = serveMapper.findServeIconCategoryByRegionId(regionId);
        if(CollUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return list;
    }

//根据id查询服务
    @Override
    public ServeAggregationSimpleResDTO serveById(Long id) {


        Serve serve = serveMapper.selectById(id);
        if (serve == null) {
            throw null;
        }
        ServeItemResDTO serveItemResDTO = serveItemService.queryServeItemAndTypeById(serve.getServeItemId());
        if (serveItemResDTO == null) {
            return null;
        }
        ServeAggregationSimpleResDTO serveAggregationSimpleResDTO=new ServeAggregationSimpleResDTO();
        serveAggregationSimpleResDTO.setId(id);
        serveAggregationSimpleResDTO.setCityCode(serve.getCityCode());
        serveAggregationSimpleResDTO.setPrice(serve.getPrice());

        serveAggregationSimpleResDTO.setServeItemName(serveItemResDTO.getName());
        serveAggregationSimpleResDTO.setServeItemId(serveItemResDTO.getServeTypeId());
        serveAggregationSimpleResDTO.setUnit(serveItemResDTO.getUnit());
        serveAggregationSimpleResDTO.setDetailImg(serveItemResDTO.getDetailImg());
        serveAggregationSimpleResDTO.setServeItemImg(serveItemResDTO.getImg());
        return serveAggregationSimpleResDTO;
    }

    //首页热门服务列表 ServeAggregationSimpleResDTO
    @Override
    public List<ServeAggregationSimpleResDTO> hotServeList(Long regionId) {
        //查询首页热门服务列表
        List<ServeAggregationSimpleResDTO> list = serveMapper.gethotServeList(regionId);
        if(CollUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return list;
    }
}
